﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerTwo : MonoBehaviour
{
    public float maxSpeedH;
    public float maxSpeedV;

    public float curSpeedH;
    public float curSpeedV;

    public bool bIsBind;

    public int hp;
    public FaceDir dir;
    public GameObject player2Project;
    public GameObject spawnPointObj;

    public AudioClip AudioClipBlock;
    public AudioClip AudioClipDie;
    
    private Rigidbody2D _rb2d;
    private Collider2D _col2d;
    private Animator _anim;
    private AudioSource _audioSource;
    private Vector2 _input;
    private SpriteRenderer _spriteRender;
    private bool bIsDead = false;

    void Awake()
    {
        _rb2d = this.GetComponent<Rigidbody2D>();
        _col2d = this.GetComponent<Collider2D>();
        _anim = this.GetComponent<Animator>();
        _audioSource = this.GetComponent<AudioSource>();
        _spriteRender = this.GetComponent<SpriteRenderer>();
    }

    private void OnEnable()
    {
        EventMgr.Register(Events.ON_PLAYER2_INPUT, OnPlayer2Input);
        EventMgr.Register(Events.ON_PLAYERS_BIND, OnPlayersBind);
    }

    private void OnDisable()
    {
        EventMgr.UnRegister(Events.ON_PLAYER2_INPUT, OnPlayer2Input);
        EventMgr.UnRegister(Events.ON_PLAYERS_BIND, OnPlayersBind);
    }

    private void OnPlayer2Input(object arg1, EventArgs arg2)
    {
        if (bIsDead)
        {
            curSpeedH = 0f;
            curSpeedV = 0f;
            return;
        }

        _input = (Vector2)arg1;

        curSpeedH = Mathf.Clamp(_input.x * maxSpeedH, -maxSpeedH, maxSpeedH);
        curSpeedV = Mathf.Clamp(_input.y * maxSpeedV, -maxSpeedV, maxSpeedV);

        if (bIsBind)
        {
            return;
        }

        if (_input.x > 0 && dir == FaceDir.FACE_DIR_LEFT)
        {
            dir = FaceDir.FACE_DIR_RIGHT;
            _spriteRender.flipX = false;
        }
        else if (_input.x < 0 && dir == FaceDir.FACE_DIR_RIGHT)
        {
            dir = FaceDir.FACE_DIR_LEFT;
            _spriteRender.flipX = true;
        }
    }

    private void OnPlayersBind(object arg1, EventArgs arg2)
    {
        bIsBind = (bool)arg1;
    }

    void FixedUpdate()
    {
        if (bIsDead)
        {
            return;
        }
        Vector2 pos = _rb2d.position;
        float x = Mathf.Clamp(pos.x + curSpeedH * Time.fixedDeltaTime, EnumBase.playerMinX, EnumBase.playerMaxX);
        float y = Mathf.Clamp(pos.y + curSpeedV * Time.fixedDeltaTime, EnumBase.playerMinY, EnumBase.playerMaxY);
        pos = new Vector2(x, y);
        _rb2d.MovePosition(pos);
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (bIsDead)
        {
            return;
        }
        if (collision.transform.CompareTag("Player1Project"))
        {
            Hit(collision.transform, collision.transform.GetComponent<PlayerOneProject>().dir);
        }
        else if (collision.transform.CompareTag("EnemyProject"))
        {
            Hit(collision.transform, collision.transform.GetComponent<EnemyProject>().dir);
        }
        else if (collision.transform.CompareTag("Enemy"))
        {
            Kill();
        }
    }

    public void Hit(Transform transform, FaceDir projectDir)
    {
        if (bIsDead)
        {
            return;
        }
        if (dir != projectDir)
        {
            Block(projectDir);
        }
        else
        {
            Kill();
        }
    }

    private void Block(FaceDir projectDir)
    {
        _anim.SetTrigger("Block");

        _audioSource.clip = AudioClipBlock;
        _audioSource.Play();

        GameObject __project = GameObject.Instantiate(player2Project, spawnPointObj.transform.position, Quaternion.Euler(Vector3.right));
        if (projectDir == FaceDir.FACE_DIR_LEFT)
        {
            __project.GetComponent<PlayerTwoProject>().InitProject(FaceDir.FACE_DIR_RIGHT);
        }
        else if (projectDir == FaceDir.FACE_DIR_RIGHT)
        {
            __project.GetComponent<PlayerTwoProject>().InitProject(FaceDir.FACE_DIR_LEFT);
        }

    }

    private void Kill()
    {
        if (bIsDead)
        {
            return;
        }

        if (PlayerController.Instance.bIsGodMode)
        {
            return;
        }

        bIsDead = true;
        _anim.SetTrigger("Die");

        _audioSource.clip = AudioClipDie;
        _audioSource.Play();

        EventMgr.Call(Events.ON_PLAYER2_DIE);
    }
}
